package com.qqtech.qquan.user.service;

import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import com.qqtech.qquan.coupon.model.CouponUser;
import com.qqtech.qquan.order.service.OrderCouponService;
import com.qqtech.qquan.user.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.community.service.CommunityJoinService;
import com.qqtech.qquan.user.cache.UserSysDictCache;
import com.qqtech.qquan.user.dao.UserBaseDao;
import com.qqtech.qquan.user.enums.UserConst;
import com.qqtech.qquan.user.enums.UserPrivacyTypeDictEnum;

import jersey.repackaged.com.google.common.collect.Lists;

@Service
public class UserBaseServiceImpl extends BaseServiceImpl<UserBase> implements UserBaseService {
	public static final Logger log = LoggerFactory.getLogger(UserBaseServiceImpl.class);

	@Resource
	private UserBaseDao userBaseDao;

	@Resource
	private UserSysDictCache userSysDictCache;

	@Resource
	private UserPrivacyService userPrivacyService;

	@Resource
	private CommunityJoinService communityJoinService;



	@Override
	protected BaseDao<UserBase> getBaseDao() {
		return userBaseDao;
	}

	@SuppressWarnings("unchecked")
	@Override
	public UserBaseVo getById(Integer id) {
		UserBaseVo userBaseVo = userBaseDao.getById(id);
		if (userBaseVo != null && userBaseVo.getSex() != null) {
			String sexDesc = userSysDictCache.dictValue(UserConst.DICTYPE_USER_BASE_SEX,
					userBaseVo.getSex().toString());
			userBaseVo.setSexDesc(sexDesc);
		}
		return userBaseVo;
	}

	@Override
	public Page<UserBaseVo> queryPageListByIdentity(UserBase query, Pageable pageable) {
		return userBaseDao.queryPageListByIdentity(query, pageable);
	}

	@Override
	public Page<UserBaseVo> queryPageList4Admin(UserBaseQuery query, Pageable pageable) {
		return userBaseDao.queryPageList4Admin(query, pageable);
	}

	@Override
	public List<UserBaseVo> queryList4Admin(UserBaseQuery query, Pageable pageable) {
		return userBaseDao.queryList4Admin(query, pageable);
	}

	@Override
	public int queryCount4Admin(UserBaseQuery query) {
		return userBaseDao.queryCount4Admin(query);
	}

	@Override
	public List<UserBaseVo> queryListByUserIds(List<Integer> userIds) {
		if (userIds == null || userIds.isEmpty()) {
			return Lists.newArrayList();
		}
		UserBaseQuery userQuery = new UserBaseQuery();
		userQuery.setContainIds(userIds);
		Pageable pageKit = new PageKit(1, Integer.MAX_VALUE, new PageOrder(Direction.ASC, "id"));
		return userBaseDao.queryList(userQuery, pageKit);
	}

	@Override
	public LinkedHashMap<Integer, UserBaseVo> queryUserMapListByUserIds(List<Integer> userIds) {
		LinkedHashMap<Integer, UserBaseVo> map = new LinkedHashMap<Integer, UserBaseVo>();
		List<UserBaseVo> userList = this.queryListByUserIds(userIds);
		if (userList != null && userList.size() > 0) {
			for (UserBaseVo user : userList) {
				if (null == map.get(user.getId())) {
					map.put(user.getId(), user);
				}
			}
		}
		return map;
	}

	@Override
	public Page<UserBaseVo> queryPageListJoinCommunity(UserBaseQuery query, Pageable pageable) {
		// 隐私设置的用户
		UserPrivacyQuery userPrivacyQuery = new UserPrivacyQuery();
		userPrivacyQuery.setType(UserPrivacyTypeDictEnum.COMMUNITY_NEIGHBOR.getCode());
		List<Integer> filterIds = userPrivacyService.queryUserIds(userPrivacyQuery);
		if (null != filterIds && filterIds.size() > 0) {
			query.setFilterIds(filterIds);
		}
		return userBaseDao.queryPageList(query, pageable);
	}

	@Override
	public int queryCountJoinCommunity(UserBaseQuery query) {
		// 隐私设置的用户
		UserPrivacyQuery userPrivacyQuery = new UserPrivacyQuery();
		userPrivacyQuery.setType(UserPrivacyTypeDictEnum.COMMUNITY_NEIGHBOR.getCode());
		List<Integer> filterIds = userPrivacyService.queryUserIds(userPrivacyQuery);
		if (null != filterIds && filterIds.size() > 0) {
			query.setFilterIds(filterIds);
		}
		return userBaseDao.queryCount(query);
	}

	@Override
	public Page<UserBaseVo> queryPageNeighbor(UserBaseQuery query, Pageable pageable) {
		// 隐私设置的用户
		UserPrivacyQuery userPrivacyQuery = new UserPrivacyQuery();
		userPrivacyQuery.setType(UserPrivacyTypeDictEnum.COMMUNITY_NEIGHBOR.getCode());
		List<Integer> filterIds = userPrivacyService.queryUserIds(userPrivacyQuery);
		if (null != filterIds && filterIds.size() > 0) {
			query.setFilterIds(filterIds);
		}
		return userBaseDao.queryPageNeighbor(query, pageable);
	}

	@Override
	public int queryCountNeighbor(UserBaseQuery query) {
		// 隐私设置的用户
		UserPrivacyQuery userPrivacyQuery = new UserPrivacyQuery();
		userPrivacyQuery.setType(UserPrivacyTypeDictEnum.COMMUNITY_NEIGHBOR.getCode());
		List<Integer> filterIds = userPrivacyService.queryUserIds(userPrivacyQuery);
		if (null != filterIds && filterIds.size() > 0) {
			query.setFilterIds(filterIds);
		}
		return userBaseDao.queryCountNeighbor(query);
	}
	@Override
	public int queryCountNeighborIsNew(UserBaseQuery query) {
		// 隐私设置的用户
		UserPrivacyQuery userPrivacyQuery = new UserPrivacyQuery();
		userPrivacyQuery.setType(UserPrivacyTypeDictEnum.COMMUNITY_NEIGHBOR.getCode());
		List<Integer> filterIds = userPrivacyService.queryUserIds(userPrivacyQuery);
		if (null != filterIds && filterIds.size() > 0) {
			query.setFilterIds(filterIds);
		}
		return userBaseDao.queryCountIsNew(query);
	}


	@Override
	public int queryCountNeighborIsNewByCommunityId(UserBaseQuery query) {
		return userBaseDao.queryCountIsNewByCommunityId(query);
	}

	@Override
	public List<UserBaseConfig> queryUserConfigProfession() {
		return userBaseDao.queryUserConfigProfession();
	}

	@Override
	public List<UserBaseConfig> queryUserConfigHobby() {
		return userBaseDao.queryUserConfigHobby();
	}

	@Override
	public List<UserBaseConfig> queryUserConfigDelicacy() {
		return userBaseDao.queryUserConfigDelicacy();
	}

	@Override
	public List<UserBaseConfig> queryUserConfigHometown() {
		return userBaseDao.queryUserConfigHometown();
	}

	@Override
	public List<UserBaseConfig> queryUserConfigEntertainment() {
		return userBaseDao.queryUserConfigEntertainment();
	}
}